/*
 * This file is part of LexiToolsMobile
 *  
 * Author: Jerzy Wawro, Jozef Kamycki
 * ver. 1.00
 * Copyright: Tenar (http://www.tenar.pl)
 *
 * Mobile LexiTools is an open source project; you can redistribute it 
 * and/or modify it under the terms of the license as published -  GPL with 
 * the following modification:  fragments of the code may be not copyleft 
 * (hidden), to protect contents of the dicionaries.
 * In this case the author of non-public fragments of the code is obliged 
 * to make available functionally equivalent code for unprotected dictionaries.
 * 
 * Mobile LexiTools is distributed WITHOUT ANY WARRANTY.
 * See the license for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with LexiToolsMobile (see: gpl.txt); if not - you can find it 
 * on web page http://www.fsf.org/, or write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package lt.util;

public class str {

    /**
     * Konwersja do ISO-5589-2 i opakowywanie
     */
    public static String convURLcode(String toCode) {

        String coded = "";

        for (int i = 0; i < toCode.length(); i++) {
            char[] source = toCode.toCharArray();
            // wykonanie URL encoding "opakowanie UTF8"
            switch (source[i]) {
                case (char) 0x105:   // �
                    coded += "%B1";// opakowanie do HTML-a
                    break;
                case (char) 0x107:    // �
                    coded += "%E6";//0x107;
                    break;
                case (char) 0x119:    // �
                    coded += "%EA";//0x119
                    break;
                case (char) 0x142:    // �
                    coded += "%B3";//)0x142;
                    break;
                case (char) 0x144:    // �
                    coded += "%D1";//0x144;
                    break;
                case (char) 0x0F3:    // �
                    coded += "%F3";//0x0F3;
                    break;
                case (char) 0x15B:    // � B6
                    coded += "%B6";//0x15B;
                    break;
                case (char) 0x17A:    // �
                    coded += "%BC";//0x17A;
                    break;
                case (char) 0x17C:    // �
                    coded += "%BF";//0x17C;
                    break;
                /**                    
                case (char)0x104:    // �
                coded += "%B1";//0x104;
                break;
                case (char)0x106:    // �
                coded += "%B1";//0x106;
                break;
                case (char)0x118:    // �
                coded += "%B1";//0x118;
                break;
                case (char)0x141:    // �
                coded += "%B1";//0x141;
                break;
                case (char)0x143:    // �
                coded += "%B1";//0x143;
                break;
                case (char)0x0D3:    // �
                coded += "%B1";//)0x0D3;
                break;
                case (char)0x15A:    // �
                coded += "%B1";//0x15A;
                break;
                case (char)0x179:    // �
                coded += "%B1";//0x179;
                break;
                case (char)0x17B:    // �
                coded += "%B1";//0x17B;
                break;
                 */
                case (char) 0x20:    // spacja
                    coded += "+";//0x17B;
                    break;
                default:
                    coded += source[i];
            }
        }
        return coded;
    }

    /**
     *   polska klawiatura programisty
     */
    public static String convProgrammersKbd(String HashedString) {

        // Translacja klucza przeszukiwania z # znak na UTF
        
        char[] source = HashedString.toCharArray();
        char[] result = HashedString.toCharArray();
        boolean HashOn = false;
        int ir;
        ir=0;
        for (int i = 0; i < HashedString.length(); i++) {
            if (!HashOn) { // nie by�o #
                if (source[i] == 0x23) { // #
                    HashOn = true;
                } else {
                        switch (source[i]) { // toLowerCase for Polish
// 0x0104 0x0106 0x0118 0x0141 0x0143 0x00D3 0x015A 0x0179 0x017B
// 0x0105 0x0107 0x0119 0x0142 0x0144 0x00F3 0x015B 0x017A 0x017C                             
                            case (char)0x0104:
                                result[ir]=(char)0x0105;
                                break;
                            case (char)0x0106:
                                result[ir]=(char)0x0107;
                                break;
                            case (char)0x0118: 
                                result[ir]=(char)0x0119;
                                break;
                            case (char)0x0141:
                                result[ir]=(char)0x0142;
                                break;
                            case (char)0x0143:
                                result[ir]=(char)0x0144;
                                break;
                            case (char)0x00D3:
                                result[ir]=(char)0x00F3;
                                break;
                            case (char)0x015A:
                                result[ir]=(char)0x015B;
                                break;
                            case (char)0x0179:
                                result[ir]=(char)0x017A;
                                break;
                            case (char)0x017B:
                                result[ir]=(char)0x017C;
                                break;
                            default:
                             if (ir!=i) { 
                                result[ir]=source[i];                                
                             }
                        }
                    ir=ir+1;
                }
            } else { // by� hash
                switch (source[i]) {
                    case (char) 0x61:
                        result[ir] = (char) 0x105; 
                        break;
                    case (char) 0x63:
                        result[ir] = (char) 0x107;
                        break;
                    case (char) 0x65:
                        result[ir] = (char) 0x119;
                        break;
                    case (char) 0x6C:
                        result[ir] = (char) 0x142;
                        break;
                    case (char) 0x6E:
                        result[ir] = (char) 0x144;
                        break;
                    case (char) 0x6F:
                        result[ir] = (char) 0x0F3;
                        break;
                    case (char) 0x73:
                        result[ir] = (char) 0x15B;
                        break;
                    case (char) 0x78:
                        result[ir] = (char) 0x17A;
                        break;
                    case (char) 0x7A:    
                        result[ir] = (char) 0x17C;
                        break;
                    default:
                        if (ir!=i) result[ir]=source[i];
                }
                ir++;
                HashOn = false;
            }
        }
        return String.valueOf(result,0,ir);
    }


    public static String convToLatin(String HashedString) {

        // program zamienia dwa pierwsze znaki na pary znak + spacja
        // dla znak�w ��ci�skich
        // oraz znak + ! dla znak�w polskich

        String result = ""; //key w ISO-8895-2
        char[] source = HashedString.toCharArray();
        for (int i = 0; i < HashedString.length(); i++) {

            if (i < 8) {
                switch (source[i]) {
                    case (char) 0x105: // �
                        result = result + (char) 0x61 + (char) 0x21;
                        break;
                    case (char) 0x107:    // �
                        result = result + (char) 0x63 + (char) 0x21;
                        break;
                    case (char) 0x119:    // �
                        result = result + (char) 0x65 + (char) 0x21;
                        break;
                    case (char) 0x142:    // �
                        result = result + (char) 0x6C + (char) 0x21;
                        break;
                    case (char) 0x144:    // �
                        result = result + (char) 0x6E + (char) 0x21;
                        break;
                    case (char) 0x0F3:    // �
                        result = result + (char) 0x6F + (char) 0x21;
                        break;
                    case (char) 0x15B:    // �
                        result = result + (char) 0x73 + (char) 0x21;
                        break;
                    case (char) 0x17A:    // � (x)
                        result = result + (char) 0x7A + (char) 0x21; //(char)0x78; // do cp
                        break;
                    case (char) 0x17C:    // �
                        result = result + (char) 0x7A + (char) 0x21;
                        break;
                    case (char) 0x104:    // �
                        result = result + (char) 0x61 + (char) 0x21; //(char)0x41 ;
                        break;
                    case (char) 0x106:    // �
                        result = result + (char) 0x63 + (char) 0x21; //(char)0x43;
                        break;
                    case (char) 0x118:    // �
                        result = result + (char) 0x65 + (char) 0x21; //(char)0x45 ;
                        break;
                    case (char) 0x141:    // �
                        result = result + (char) 0x6C + (char) 0x21; //(char)0x4C ;
                        break;
                    case (char) 0x143:    // �
                        result = result + (char) 0x6E + (char) 0x21; //(char)0x4E ;
                        break;
                    case (char) 0x0D3:    // �
                        result = result + (char) 0x6F + (char) 0x21; //(char)0x4F ;
                        break;
                    case (char) 0x15A:    // �
                        result = result + (char) 0x73 + (char) 0x21; //(char)0x53;
                        break;
                    case (char) 0x179:    // �
                        result = result + (char) 0x7A + (char) 0x21; //(char)0x58 ;
                        break;
                    case (char) 0x17B:    // �
                        result = result + (char) 0x7A + (char) 0x21; //(char)0x5A ;
                        break;
                    default:
                        result = result + source[i] + (char) 0x20;
                }

            } else {
                result = result + source[i];
            }
        }
        return result;
    }
}